.SILENT:
all: help


CURRENT_DIR := $(abspath $(lastword $(MAKEFILE_LIST)))
PROJECT_NAME := $(notdir $(patsubst %/,%,$(dir $(CURRENT_DIR))))
OUTPUT:="${HOME}/tmp/cmake_builds/${PROJECT_NAME}"


open_output_dir:
	@if [ "$(OS)" = "Windows_NT" ]; then \
		start $(OUTPUT); \
	elif [ "$(shell uname)" = "Darwin" ]; then \
		open $(OUTPUT); \
	elif [ "$(shell uname)" = "Linux" ]; then \
		xdg-open $(OUTPUT) || nautilus $(OUTPUT) || dolphin $(OUTPUT); \
	else \
		echo "❌ 警告：不支持的操作系统，无法打开构建目录"; \
	fi


## build@编译程序
.PHONY: build
build:
	@cmake -B ${OUTPUT} -DCMAKE_BUILD_TYPE=DEBUG
	@cd ${OUTPUT} && make -j8
	@echo
	@echo ${OUTPUT}
	@ls -lh --color=auto ${OUTPUT}
	$(MAKE) open_output_dir
	@echo ""


## run@运行程序
run: 
	@cd ${OUTPUT} && ./soeg_server


## test@运行全部测试用例
.PHONY:test
test:
	@echo "\033[33m🧪  正在测试...\033[0m"
	@cd ${OUTPUT} && make test


## testx name=<target>@运行目标测试程序
# 如： make testx name=multicast_test
.PHONY:testx
testx:
	@cd ${OUTPUT} && "./tests/$(name)"


## clean@清理缓存
clean:
	@rm -rf ${OUTPUT}/*
	@rm -rf ./logs log temp tmp
	@rm -rf ./cmd/*.exe ./cmd/*.bin


## push@推送到远程仓库
.PHONY:push
push:
	@echo "\033[0;32mDeploying updates to GitHub...\033[0m";
	@git add . ;
	@msg="Rebuild at $$(date)";git commit -m "$$msg";
	@git push origin master
	@echo "源码推送成功"


## help@查看帮助
help: Makefile
	@echo "Usage:  make [command] [options]"
	@echo
	@echo "Available Commands:"
	@sed -n "s/^##//p" $< | column -t -s '@' | grep --color=auto -e "^[[:space:]][a-z]\+[[:space:]]" -e "[a-z]\+=[a-z\<\>\_]\+"
	@echo
	@echo "For more to see https://github.com/hollson"
	@echo
